<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>JSP</title>
    <link rel="stylesheet" href="../../css/base.css" />
	<link rel="stylesheet" href="../../css/info-mgt.css" />
	<link rel="stylesheet" href="../../css/info-reg.css" />
	<link rel="stylesheet" href="../../css/WdatePicker.css" />
	<script type="text/javascript" src="../../js/jquery.js"></script>
	<script type="text/javascript" src="../../js/common.js"></script>
	<script type="text/javascript" src="../../js/WdatePicker.js"></script>
	<script type="text/javascript" src="../../js/jquery.pagination.js"></script>
	<script type="text/javascript" src="../../js/ajaxfileupload.js"></script>
  </head>
  
  <body>
    <div class="title"><h2>个人文件中心</h2></div>
	<div class="query">
		<div class="query-conditions ue-clear">
			<div class="conditions name ue-clear" style="width:20%;">
	            <label>文档主题:</label>
	            <div class="select-wrap">
	                <div class="select-title ue-clear">
	                	<!-- 标题text文本框 -->
	                	<input type="text" id="title" style="width:100%; border:0px solid red;" placeholder="请输入姓名" />
	                </div>
	            </div>
	        </div>
	        <div class="conditions time ue-clear" style="width:19%;">
	            <label>上传时间:</label>
	            <div class="time-select">
	            	<input id="startTime" type="text" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" placeholder="开始时间" />
	                <i class="icon"></i>
	            </div>
	            <span class="line">-</span>
	            <div class="time-select">
	            	<input id="endTime" type="text" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" placeholder="结束时间" />
	                <i class="icon"></i>
	            </div>
	        </div>
	    </div>
	    <div class="query-btn ue-clear">
	    	<a href="javascript:;" class="confirm">查询</a>
	        <a href="javascript:;" class="clear">清空条件</a> 
	    </div>
	</div>
	<div class="table-box">
		<table>
	    	<thead>
	        	<tr>
	            	<th class="num">上传时间</th>
	                <th class="name">文档主题</th>
	                <th class="operate">操作</th>
	            </tr>
	        </thead>
	        <tbody id="FilesTbody">
	        	<!-- 个人文件表格 -->
	        </tbody>
	    </table>
	</div>
	<!-- 分页按钮 ------------------------------------------------->
	<div style="margin:0px auto; width:100%; text-align:center;">
	  	<table id="tableLast" width="80%" align="center" border="0">
	  		<tr align="center">
	  			<td width="75%"></td>
	  			<td><a href="javascript:void(0);" id="headPage">首页</a></td>
	  			<td><a href="javascript:void(0);" id="backPage">上一页</a></td>
	  			<td align="right">当前页面:</td>
	  			<td width="8%" align="left"><input type="text" id="page" value="1" style="width:20%;">
	  				<a href="javascript:void(0);" id="skipPage">跳转</a>&nbsp;&nbsp;
	  				<label id="nowPage">1</label>/<label id="countPage"></label>
	  			</td>
	  			<td><a href="javascript:void(0);" id="nextPage">下一页</a></td>
	  			<td><a href="javascript:void(0);" id="lastPage">末页</a></td>
	  		</tr>
	  	</table>
	  	<br/>
  	</div>
  	
  	<!-- 上传文件 -->
	<div class="title" id="noticeTopic1"><h2>上传文件</h2></div>
	<div class="main" id="noticeTopic2">
		<p class="short-input ue-clear">
	    	<label>选择文件:</label>
	    	<input type="file" accept=".doc,.docx" id="file" name="file" />
	    </p>
	    <p class="long-input ue-clear">
	    	<label>文件类型:</label>
	        <select id="fileType">
	        	<option value="0">--请选择--</option>
	        	<option value="person">个人</option>
	        	<option value="dept">共享</option>
	        	<option id="fileCom" value="company">单位</option>
	        </select>
	    </p>
	    <p id="deptP" class="short-input ue-clear" style="display:none;">
	    	<label>共享部门:</label>
	    	<input type="text" id="deptText" />
	    	<select id="selDept">
	    		<option value=0>--请选择--</option>
	    		<option value="cash">财务部</option>
	    		<option value="manager">总经理办公室</option>
	    		<option value="search">研发部</option>
	    		<option value="people">人力资源部</option>
	    		<option value="market">市场部</option>
	    		<option value="service">后勤部</option>
	    	</select>
	    </p>
	</div>
	<div class="btn ue-clear" id="noticeTopic3">
		<a href="javascript:;" class="confirm" id="upload">上传</a>
	</div>
  	
  	<script type="text/javascript">
  		//共享
  		function shareFile(i){
  			var str="";
  			str+="<p id='deptS"+i+"' class='short-input ue-clear' style='float:right;'>";
  			str+="<label>共享部门:</label><input type='text' id='deptSText"+i+"' />";
  			str+="<select id='selSDept"+i+"' onchange='addDept("+i+");'><option value=0>--请选择--</option>";
	    	str+="<option value='cash'>财务部</option>";
	    	str+="<option value='manager'>总经理办公室</option>";
	    	str+="<option value='search'>研发部</option>";
	    	str+="<option value='people'>人力资源部</option>";
	    	str+="<option value='market'>市场部</option>";
	    	str+="<option value='service'>后勤部</option>";
	    	str+="</select></p>";
	    	$("#sha"+i).replaceWith("<a id='save"+i+"' onclick='save("+i+");' href='javascript:void(0);'>保存</a>");
  			$("#del"+i).replaceWith("<a id='cancel"+i+"' onclick='cancel("+i+");' href='javascript:void(0);'>取消</a>");
  			$("#cancel"+i).after(str);
  		}
  		//取消
  		function cancel(i){
  			$("#save"+i).replaceWith("<a id='sha"+i+"' href='javascript:void(0);' onclick='shareFile("+i+")' >共享<a/>");
  			$("#cancel"+i).replaceWith("<a id='del"+i+"' href='javascript:void(0);' onclick='delFile("+i+");' >删除<a/>");
  			$("#deptS"+i).remove();
  		}
  		//添加共享部门
  		function addDept(i){
  			var deptName=$("#selSDept"+i).find("option:selected").text();
	  		if(deptName!="--请选择--"&&$("#deptSText"+i).val().indexOf(deptName)==-1){
	  			$("#deptSText"+i).val($("#deptSText"+i).val()+deptName+";");
	  		}
  		}
  		//保存
  		function save(i){
  			if($("#deptSText"+i).val()!=""){
  				if(window.confirm("确认更改为共享文件吗?")){
  					var id=$("#fileId"+i).val();
  					var deptStr=$("#deptSText"+i).val();
  					//异步更改
  					$.ajax({
  					 type:"POST",
  					 url:"filesCenterManager.action?method=updateFile",
  					 data:"id="+id+"&deptStr="+deptStr,
  					 success:function(msg){
  					 	alert("共享成功");
  					 	$("#fileTr"+i).replaceWith("<tr><td class='name'> - </td></tr>");
  					 }
  					 
  					});
  				}
  			}
  		}
	  	
  		
  		
  		
  		
  		
  		
  		//删除
	  	function delFile(i){
	  		var id=$("#fileId"+i).val();
	  		var filePath=$("#fileName"+i).attr("href");
	  		var fileName=filePath.substring(filePath.lastIndexOf("/")+1,filePath.length);
	  		if(window.confirm("确认删除该文件吗?")){
	  			$.ajax({
	  			 type:"POST",
	  			 url:"filesCenterManager.action?method=delFile",
	  			 data:"id="+id+"&fileName="+fileName,
	  			 success:function(msg){
	  			 	alert("删除成功");
	  			 	
	  			 	$("#fileTr"+i).replaceWith("<tr><td class='name'> - </td></tr>");
	  			 }
	  			});
	  		}
	  	}
  	
  	$(function(){
  		//文件类型
	  	$("#fileType").change(function(){
	  		if($("#fileType").find("option:selected").val()=="dept"){
	  			$("#deptP").show("slow");
	  		}else{
	  			$("#deptP").hide("slow");
	  		}
	  	});
	  	//共享部门
	  	$("#selDept").change(function(){
	  		var deptName=$("#selDept").find("option:selected").text();
	  		if(deptName!="--请选择--"&&$("#deptText").val().indexOf(deptName)==-1){
	  			$("#deptText").val($("#deptText").val()+deptName+";");
	  		}
	  	});
	  	//文件选择
	  	var isChoose=false;
	  	$("#file").change(function(){
	  		isChoose=true;
	  	});
	  	
	  	//上传
	  	$("#upload").click(function(){
	  		if(!isChoose){
	  			alert("请选择上传文件");
	  			return;
	  		}
	  		var type=$("#fileType").find("option:selected").val();
	  		var deptStr="";
	  		if(type==0){
	  			alert("请选择文件类型");
	  			return;
	  		}
	  		if(type=="dept"){
	  			deptStr=$("#deptText").val();
	  			if(deptStr==""){
	  				alert("请选择共享部门");
	  				return;
	  			}
	  		}
	  		if(window.confirm("确认上传该文件吗?")){
	  			//异步上传
	  			$.ajaxFileUpload({
	  			 url:"filesCenterManager.action?method=fileUpLoad&type="+type+"&deptStr="+deptStr,
	  			 fileElementId:"file",
	  			 success:function(msg){
	  			 	alert("上传成功");
	  			 	//异步 刷新表格 调用分页
					condition="nowPage=0&turnType=next";
					paging(condition);
					//清空数据
					$("#file").replaceWith("<input type='file' accept='.doc,.docx' id='file' name='file' />");
	  			 	$("#fileType").find("option[value=0]").attr("selected",true);
	  			 	$("#selDept").find("option[value=0]").attr("selected",true);
	  			 	$("#deptText").val("");
	  			 	$("#deptP").hide("slow");
	  			 }
	  			});
	  		}
	  	});
	  	
	  	//查询字符串
  		var condition="";
  		var username="${login_User.userName }";
  		var term="";
  		var other="&file_type=person&file_username="+username;
		//异步查找个人文档
		function paging(condition){
			if(isCondition){
				var title=$("#title").val();
				var startTime=$("#startTime").val();
				var endTime=$("#endTime").val();
				if(title!=""){
					term+="&file_topic="+title;
				}
				//拼接时间查询字符串
				if(startTime!=""&&endTime!=""){
					term+="&file_time="+startTime+","+endTime;
				}else if(startTime!=""&&endTime==""){
					term+="&file_time="+startTime+",8888-08-08";
				}else if(startTime==""&&endTime!=""){
					term+="&file_time=1111-11-11,"+endTime;
				}
			}
			$.ajax({
			 type:"POST",
			 url:"accLwg*FileDocument.paging",
			 data:condition+other+term,
			 success:function(msg){
			 	term="";
			 	var str="";
			 	for(var i=0;i<msg.data.length;i++){
			 		str+="<tr id='fileTr"+i+"'><input type='hidden' id='fileId"+i+"' value='"+msg.data[i].fileId+"' />";
			 		str+="<td width='35%' class='num'>"+msg.data[i].fileTime+"</td>";
			 		str+="<td width='35%' class='name'><a id='fileName"+i+"' href='${pageContext.request.contextPath }/"+msg.data[i].fileText+"'>"+msg.data[i].fileTopic+"</a></td>";
		            str+="<td class='operate'><a id='sha"+i+"' href='javascript:void(0);' onclick='shareFile("+i+")' >共享<a/>&nbsp;&nbsp;&nbsp;";
		            str+="<a id='del"+i+"' href='javascript:void(0);' onclick='delFile("+i+");' >删除<a/></td></tr>";
			 	}
			 	if(i<10){
			 		for(;i<10;i++){
			 			str+="<tr><td class='name'> - </td></tr>";
			 		}
			 	}
			 	$("#FilesTbody").html(str);
			 	//向上取整总页数
				$("#countPage").text(Math.ceil(msg.rowNum/15));
			 }
			});
		}
		//调用分页
		condition="nowPage=0&turnType=next";
		paging(condition);
		
		
		//首页
		$("#headPage").click(function(){
			if($("#nowPage").text()==1){
			}else{
				data="nowPage=0&turnType=next";
				paging(data);
				$("#nowPage").text(1);
				$("#page").val(1);
			}
		});
		//末页
		$("#lastPage").click(function(){
			if($("#nowPage").text()==$("#countPage").text()){
			}else{
				data="nowPage="+(parseInt($("#countPage").text())-1)+"&turnType=next";
				paging(data);
				$("#nowPage").text($("#countPage").text());
				$("#page").val($("#nowPage").text());
			}
		});
		//上一页
		$("#backPage").click(function(){
			if($("#nowPage").text()==1){
			}else{
				data="nowPage="+$("#nowPage").text()+"&turnType=back";
				paging(data);
				$("#nowPage").text(parseInt($("#nowPage").text())-1);
				$("#page").val($("#nowPage").text());
			}
		});
		//下一页
		$("#nextPage").click(function(){
			if($("#nowPage").text()==$("#countPage").text()){
			}else{
				data="nowPage="+$("#nowPage").text()+"&turnType=next";
				paging(data);
				$("#nowPage").text(parseInt($("#nowPage").text())+1);
				$("#page").val($("#nowPage").text());
			}
		});
		//跳转页面
		$("#skipPage").click(function(){
			if($("#page").val()>$("#countPage").text()||$("#page").val()<1||
			$("#page").val()==$("#nowPage").text()){
			}else{
				data="nowPage="+(parseInt($("#page").val())-1)+"&turnType=next";
				paging(data);
				$("#nowPage").text($("#page").val());
			}
		});
		
		//清空按钮
		$(".clear").click(function(){
			$("#title").val("");
			$("#startTime").val("");
			$("#endTime").val("");
			isCondition=false;
			
		});
		var isCondition=false;//是否使用查询条件
		//查询按钮
		$(".confirm").click(function(){
			//调用分页
			isCondition=true;
			condition="nowPage=0&turnType=next";
			paging(condition);
		});
	});	
  		
  		
  		
  	//如果登录部位总经理 不能上传单位文件
  	if("${login_User.userDeptId }"!="2"){
  		$("#fileCom").hide();
  	}
  	
  		
  	//改变分页按钮字体大小
	$("#tableLast tr td").css("font-size","15px");
	$("#tableLast tr td").css("background","url(../../images/righttitlebig.png)");
	
	$("tbody").find("tr:odd").css("backgroundColor","#eff6fa");
	
	showRemind('input[type=text], textarea','placeholder');
	//样式
	$("tbody").find("tr:odd").css("backgroundColor","#eff6fa");
	showRemind('input[type=text], textarea','placeholder');
  	</script>
  	
  	
  </body>
</html>
